(function () {
  let runOnce = function (cb) {
    let num = 0

    return function () {
      if (num < 1) {
        num = num++
        cb && cb()
      }
    }
  }

  let k = 'test-s'
  let runId = 'a6'
  let vvg = window.vvg
  vvg.elements[runId] = {
    attrs: {
      'data-com-type': k,
      'data-can-child': "true",
      'data-run-id': runId
    },
    textContent: k,
    run : runOnce(function () {
      window.getVma('pageapp').$refs.com.initExt(k, (vvg.page.ext[k].toString()))
    })
  }


  vvg.page.ext[k] = `
<template id="${k}-tpl">
  <div class="footer" >
  footer
    <a href="javascript:;" @click="handleClick(0)"></a>
    <a href="javascript:;" @click="handleClick(1)"></a>
  </div>
</template>
<template id="${k}-style-tpl">
.footer {
  width: 7.5rem;
}
.footer a {
  display: block
}
</template>
<script>
(function() {
if (window.addStyleSheetFromTemplate) {
   window.addStyleSheetFromTemplate('${k}')
}

Vue.component('${k}', {
 template: '#'+'${k}'+'-tpl',
  props: {
    sstyle: {
      type: String,
      default: JSON.stringify({})
    },
    link: String,
    ssurl: String
  },
  watch: {
    sstyle(newVal) {
      this.genStyle(newVal)
    }
  },
  mounted() {
   this.genStyle(this.sstyle)
   this.genScr(this.link)
   console.log(this.link, this.ssurl)
  },
  methods: {
    genStyle(sstyle) {
      if (window.cssobj) {     
        window.cssobj(JSON5.parse(sstyle)) 
      }
    },
    genScr(link) {
        function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)")
    var r = window.location.search.substr(1).match(reg)
    if (r != null) return unescape(r[2] )
    return null
  }
  var source = getQueryString('source') || ''
      
      $.get(link + '?source=' + source).done(function (res_get) {

        $('.footer a:eq(0)').on('click', function () {
            _hmt.push(['_trackEvent', document.title + '('+this.ssurl+')', '首页(button1)', source])
            location.href = res_get.jumplink[0]
        })
        $('.footer a:eq(1)').on('click', function () {
            _hmt.push(['_trackEvent', document.title + '('+this.ssurl+')', '首页(button2)', source])
            location.href = res_get.jumplink[1]
        })
        
        // 微信客户端
        let ua = navigator.userAgent.toLowerCase()
        if (ua.match(/MicroMessenger/i) == "micromessenger") {
            $.post('http://market.kaoyanvip.cn/api/wechat/').then(res => {
                wx.config({
                    debug: false,
                    appId: res.appID,
                    timestamp: res.timestamp,
                    nonceStr: res.noncestr,
                    signature: res.signature,
                    jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage']
                })
                wx.ready(function () {
                    wx.onMenuShareTimeline({
                        title: res_get.share.title,
                        link: location.href,
                        imgUrl: res_get.share.imgUrl
                    })
                    wx.onMenuShareAppMessage({
                        title: res_get.share.title,
                        desc: res_get.share.desc,
                        link: location.href,
                        imgUrl: res_get.share.imgUrl
                    })
                })
            })
        }
    })

    },
    handleClick() {
      
    }
  }
})
})()
</script>
    `.trim()

  vvg.page.des[k] = function() {
    return {
      '.footer': {
        type: 'style',
        value: '',
        for: 'sstyle',
      },
      '.footer > a:nth-child(1)': {
        type: 'style',
        value: '',
        for: 'sstyle',
      },
      '.footer > a:nth-child(2)': {
        type: 'style',
        value: '',
        for: 'sstyle',
      }
    }
  }

  console.log('run ', k)
})()
